package com.manyiaby.view.viewutils;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

/**
 * Created by yx on 2017/7/15.
 */

public abstract class NoDoubleClickListener implements View.OnClickListener {

    public static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        LogUtil.iYx("----当前时间--:" + currentTime + "----上次时间--:" + lastClickTime + "----时间差--:" + (currentTime - lastClickTime));
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            LogUtil.iYx("------调用代码执行打开-------");
            lastClickTime = currentTime;
            onNoDoubleClick(v);
            return;
        }
    }

    protected abstract void onNoDoubleClick(View v);


    /**
     * 判断是否是连续快速点击
     *
     * @return
     */
    private static long mLastClickTime = 0;//防止连续快速点击

    public static boolean isFastClick() {
        // 当前时间
        long currentTime = System.currentTimeMillis();
        // 两次点击的时间差
        long time = currentTime - mLastClickTime;
        if (time > 0 && time < 1000) {
            return true;//多次点击
        }
        mLastClickTime = currentTime;
        return false;//一次点击
    }


    /**
     * 是单击，还是双击
     */
    private static int count;// 计算点击的次数
    private static long curTime = 0;//当前时间
    private static long lastClick = 0;//最后一次点击时间
    private static long CLICK_SPACING_TIME = 300;//间隔
    private static ClicksCallback clicksCallback;

    public interface ClicksCallback {
        void isDouble();

        void isclickSingle();
    }

    // 使用handler时首先要创建一个handler
    private static Handler handler = new Handler();

    private static Runnable mRunnable = new Runnable() {

        @Override
        public void run() {
            if (count <= 1) {
                //单击
                //Log.i("aaaa", "========单机了count============" + count);
                clicksCallback.isclickSingle();
                count = 0;
            } else {
                //双击
                //Log.i("aaaa", "========双击了count============" + count);
                clicksCallback.isDouble();
                count = 0;
            }
        }
    };

    public static void isDoubleClick(ClicksCallback clicksCallbacks) {
        clicksCallback = clicksCallbacks;
        lastClick = curTime;//记录最后一次点击时间
        // 当前时间
        curTime = System.currentTimeMillis();

        if (curTime - lastClick < CLICK_SPACING_TIME) {//双击了
            handler.removeCallbacks(mRunnable);//取消第二次点击的线程
            //Log.i("aaaa", "========双击了，取消第二次点击线程============");
        }
        count++;
        //Log.i("aaaa", "========count============" + count);
        handler.postDelayed(mRunnable, CLICK_SPACING_TIME);//延迟CLICK_SPACING_TIME后运行run
    }


    /**
     * 双击事件、多击事件（无法判断是否是单机）
     */
    //存储时间的数组
    static long[] mHits = new long[2];

    public static void doubleClick() {
        // 双击事件响应
        /**
         * arraycopy,拷贝数组
         * src 要拷贝的源数组
         * srcPos 源数组开始拷贝的下标位置
         * dst 目标数组
         * dstPos 开始存放的下标位置
         * length 要拷贝的长度（元素的个数）
         *
         */
        //实现数组的移位操作，点击一次，左移一位，末尾补上当前开机时间（cpu的时间）
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        //双击事件的时间间隔500ms
        if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
            //双击后具体的操作
            //do
            ToastUtils.showToastApplication("双击");
        }
    }


}
